package com.jzg.jcptadmin.view;

import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.ScreenUtils;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.data.vo.PopListDataModel;
import com.jzg.template.refreshrecycleview.DividerItemDecoration;
import com.jzg.template.refreshrecycleview.RecycleViewBaseAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jzg on 2015/12/23.
 */
public class PopWindowView implements PopupWindow.OnDismissListener {
    private BaseActivity context;
    private PopupWindow mPopWindow;
    private RelativeLayout mPopContainer;
    private List<PopListDataModel> mSortList = new ArrayList<>();
    private int mFastSortIndex = 0;
    private Map<TextView, Integer> indexMap = new HashMap<>();
    View fastHintline;
    TextView filter_status;
    TextView filter_date;
    TextView filter_area;
    TextView filter_pinpai;
    TextView filter;
    Drawable drawable;

    public PopWindowView(BaseActivity aty, TextView tv1, TextView tv2, View fastHintline) {
        this.context = aty;
        this.filter_date = tv1;
        this.filter_status = tv2;
        initPopWindow();
        this.fastHintline = fastHintline;
    }

    public PopWindowView(BaseActivity aty, TextView tv1, TextView tv2, View fastHintline, Drawable drawable) {
        this.context = aty;
        this.filter_date = tv1;
        this.filter_status = tv2;
        initPopWindow();
        this.fastHintline = fastHintline;
        this.drawable = drawable;
    }

    public PopWindowView(BaseActivity context, TextView tv1, TextView tv2, TextView tv3, TextView tv4, View fastHintline) {
        this.context = context;
        this.filter_pinpai = tv1;
        this.filter_date = tv2;
        this.filter_area = tv3;
        this.filter_status = tv4;
        initPopWindow();
        this.fastHintline = fastHintline;
    }

    public void setData(List<PopListDataModel> data) {
        mSortList = data;
    }

    private void initPopWindow() {
        if (mPopWindow == null) {
            mPopWindow = new PopupWindow();
            View contentView = LayoutInflater.from(context).inflate(
                    R.layout.view_fastline_pop, null);
            mPopContainer = (RelativeLayout) contentView
                    .findViewById(R.id.view_fastline_popWindow);
            contentView.findViewById(R.id.view_fastline_out_popWindow)
                    .setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            mPopWindow.dismiss();
                        }
                    });
            mPopWindow.setContentView(contentView);
            mPopWindow.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            mPopWindow.setBackgroundDrawable(new ColorDrawable(context.getResources()
                    .getColor(android.R.color.transparent)));
        }
        mPopWindow.setOnDismissListener(this);
        mPopWindow.setFocusable(true);
        mPopWindow.setOutsideTouchable(true);
    }

    public void toChangeFilterBtnColor(TextView blue, TextView grey) {
        if (blue != null) {
            filter = blue;
            blue.setTextColor(context.getResources().getColor(R.color.text_blue));
        }
        if (grey != null) {
            grey.setTextColor(context.getResources().getColor(
                    R.color.text_filter_darkgrey));
        }
    }

    public void toChangeFilterBtnColor(TextView blue, TextView grey1, TextView grey2, TextView grey3) {
        if (blue != null) {
            filter = blue;
            blue.setTextColor(context.getResources().getColor(R.color.text_blue));
        }
        if (grey1 != null) {
            grey1.setTextColor(context.getResources().getColor(
                    R.color.text_filter_darkgrey));
        }
        if (grey2 != null) {
            grey2.setTextColor(context.getResources().getColor(
                    R.color.text_filter_darkgrey));
        }
        if (grey3 != null) {
            grey3.setTextColor(context.getResources().getColor(
                    R.color.text_filter_darkgrey));
        }
    }

    public void controlPopWindowShow(TextView blue, TextView grey) {

        if (mPopContainer != null)
            mPopContainer.removeAllViews();
        if (indexMap.containsKey(blue)) {
            mFastSortIndex = indexMap.get(blue);
        } else {
            mFastSortIndex = 0;
        }
        mPopContainer.addView(initPopWindowView(mPopContainer));
        //Android 7.0 PopupWindow 弹出位置异常情况
        if (Build.VERSION.SDK_INT == 24) {
            Rect visibleFrame = new Rect();
            fastHintline.getGlobalVisibleRect(visibleFrame);
            mPopWindow.showAtLocation(fastHintline, Gravity.NO_GRAVITY, 0, visibleFrame.bottom);
        } else {
            mPopWindow.showAsDropDown(fastHintline);
        }
        toChangeFilterBtnColor(blue, grey);
    }

    public void controlPopWindowShow(TextView blue, TextView grey1, TextView grey2, TextView grey3) {
        if (mPopContainer != null)
            mPopContainer.removeAllViews();
        //如果不存在blue,则把index置0，如果存在，
        if (indexMap.containsKey(blue)) {
            mFastSortIndex = indexMap.get(blue);
        } else {
            mFastSortIndex = 0;
        }
        mPopContainer.addView(initPopWindowView(mPopContainer));
        //Android 7.0 PopupWindow 弹出位置异常情况
        if (Build.VERSION.SDK_INT == 24) {
            Rect visibleFrame = new Rect();
            fastHintline.getGlobalVisibleRect(visibleFrame);
            mPopWindow.showAtLocation(fastHintline, Gravity.NO_GRAVITY, 0, visibleFrame.bottom);
        } else {
            mPopWindow.showAsDropDown(fastHintline);
        }
        toChangeFilterBtnColor(blue, grey1, grey2, grey3);
    }

    public void controlPopWindowDismiss(int position) {
        mPopWindow.dismiss();
        filter.setText(mSortList.get(mFastSortIndex).getText());
        context.filter_date(filter.getId(), position);
    }

    private View initPopWindowView(ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_pop_recycleview, parent, false);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.fast_pop_recycler);
        recyclerView.addItemDecoration(new DividerItemDecoration(context, MyLinearLayoutManager.VERTICAL));
        Adapter adapter = new Adapter();
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new MyLinearLayoutManager(context));
        initListItemColor(mSortList, mFastSortIndex);
        adapter.setOnItemClickLitener(new RecycleViewBaseAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                mFastSortIndex = position;
//                indexMap.clear();
                indexMap.put(filter, mFastSortIndex);
                // 请求排序列表
                controlPopWindowDismiss(position);
            }
        });
        return view;
    }

    class Adapter extends RecycleViewBaseAdapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            TextView textView = new TextView(context);
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtils.dip2px(context, 40)));
            textView.setGravity(Gravity.CENTER_VERTICAL);
            return new MyViewHolder(textView);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);
            MyViewHolder molder = (MyViewHolder) holder;
            molder.tv.setText(mSortList.get(position).getText());
            molder.tv.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.item_pressed));
            molder.tv.setTextColor(context.getResources().getColor(
                    mSortList.get(position).getTextColor()));
        }

        @Override
        public int getItemCount() {
            return mSortList.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            TextView tv;

            public MyViewHolder(View view) {
                super(view);
                tv = (TextView) view;
            }
        }
    }

    private void initListItemColor(List<PopListDataModel> list, int index) {
        for (int i = 0; i < list.size(); i++) {
            if (i == index) {
                list.get(i).setTextColor(R.color.text_blue);
            } else {
                list.get(i).setTextColor(R.color.light_grey_text);
            }
        }
    }

    @Override
    public void onDismiss() {
        toChangeFilterBtnColor(null, filter_date);
        toChangeFilterBtnColor(null, filter_status);
        toChangeFilterBtnColor(null, filter_area);
        toChangeFilterBtnColor(null, filter_pinpai);
        //箭头向下
        filter_status.setCompoundDrawables(null, null, drawable, null);//画在右边
        filter_date.setCompoundDrawables(null, null, drawable, null);//画在右边
    }

}